using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data;
using Spring.Data.NHibernate.Support;
using BusinessLayer.Model;

namespace BusinessLayer
{
    /// <summary>
    /// Data Access Object odpowiedzialny za komunikacje z warstwa skladu danych - w tym
    /// przypadku jest to biblioteka NHibernate
    /// </summary>
    public class TPBWyszukiwarkaDao : HibernateDaoSupport
    {
        /// <summary>
        /// Metoda dodajaca do bazy danych strone WWW
        /// </summary>
        /// <param name="page"></param>
        public void AddWebPage(WebPage page)
        {           
            Session.SaveOrUpdateCopy(page);
            //Session.SaveOrUpdate(page);
        }

        /// <summary>
        /// Metoda czyszczaca dotychczasowe dane indeksu podanej strony WWW
        /// </summary>
        /// <param name="page"></param>
        public void ClearWebPageIndex(WebPage page)
        {
            NHibernate.IQuery query = Session.CreateQuery("from WebPage where Url = :url");
            query.SetString("url", page.Url);
            IList<WebPage> pages = query.List<WebPage>();
            foreach (WebPage p in pages)
            {
                Session.Delete(p);
            }
            Session.Flush();
        }

        /// <summary>
        /// Metoda dodajaca podane slowo do bazy danych
        /// </summary>
        /// <param name="word"></param>
        public void AddWord(Word word)
        {
            HibernateTemplate.SaveOrUpdateCopy(word);
            //Session.SaveOrUpdate(word);
        }

        /// <summary>
        /// Metoda wyszukujaca elementy indeksu przypisane do podanego slowa
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public IList<IndexElement> FindIndexElementsByWord(string word)
        {
            NHibernate.IQuery query = Session.CreateQuery("from IndexElement ie where ie.Word.Text = :word");
            query.SetString("word", word);
            IList<IndexElement> result = query.List<IndexElement>();

            return result;
        }
    }
}
